package com.lml.hydrogenexpertsystem.backend.controller;


import com.lml.hydrogenexpertsystem.backend.common.BaseResponse;
import com.lml.hydrogenexpertsystem.backend.common.ResultUtils;
import com.lml.hydrogenexpertsystem.backend.exception.ErrorCode;
import com.lml.hydrogenexpertsystem.backend.exception.ThrowUtils;
import com.lml.hydrogenexpertsystem.backend.model.dto.UserLoginRequest;
import com.lml.hydrogenexpertsystem.backend.model.dto.UserRegisterRequest;
import com.lml.hydrogenexpertsystem.backend.model.vo.LoginUserVo;
import com.lml.hydrogenexpertsystem.backend.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 梦龙
 * @date 2025/4/4 19:57
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * @description: 用户注册
     * @Param:
     * @return:
     * @time: 2025/4/4 20:19
     **/
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        String account = userRegisterRequest.getAccount();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long result = userService.userRegister(account, password, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * @description: 用户登陆
     * @Param:
     * @return:
     * @time: 2025/4/4 20:19
     **/
    @PostMapping("/login")
    public BaseResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginRequest.getAccount();
        String userPassword = userLoginRequest.getPassword();
        LoginUserVo loginUserVo = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVo);
    }

    /**
     * @description:用户登出
     * @param:
     * @return:
     * @time: 2025/4/12 22:27
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        Boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

}
